<?php
namespace Home\Controller;
class AiadminController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}


	//自媒体列表
	public function aiadmin_list()
	{
		$phone = $_GET['phone'];
		$code = $_POST['param'];
		$db_aiadmin = M("aiadmin");
		$count = $db_aiadmin->count();
		$p = getpage($count, 20);
		$aiadminlist = $db_aiadmin->limit($p->firstRow, $p->listRows)->select();

		// p($aiadminlist);exit();
		$this->assign('aiadminlist',$aiadminlist);
		$this->assign('page', $p->show());
		$this->display();
	}

	//创建自媒体
	public function aiadmin_add()
	{
		$this->display();
	}

	//待审核详情
	public function add_aiadmin()
	{
		// p($_POST);exit();
		$username = I('post.username');
		$password = I('post.password');
		$type = I('post.type');
		if (empty($username)||empty($password)) {
			$this->error('参数错误',"",1);
		}

		$aiadmin = D("aiadmin");
		if (!$aiadmin->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $aiadmin->create(); // 保存生成的数据对象

			$data['addtime'] = date('Y-m-d H:i:s',time());
			$data['username'] = $username;
			$data['password'] = md5($password);
			$data['articles'] = $type;
			// p($data);exit();
			$result = $aiadmin->add($data);
			$this->success('创建成功',"/Home/Aiadmin/aiadmin_list",1);
		}

	}


	//修改标签
	public function aiadmin_edit()
	{
		$id = I('get.id');
		$db_aiadmin = M("aiadmin");
		$aiadmindata = $db_aiadmin->where(array('id'=>$id))->find();
		// p($aiadmindata);exit();
		$this->assign('aiadmindata',$aiadmindata);
		$this->display();
	}

	public function edit_aiadmin()
	{
		$id = I('post.id');
		$username = I('post.username');
		$password = I('post.password');
		$type = I('post.type');
		if (empty($username)) {
			$this->error('参数错误',"",1);
		}

		$aiadmin = D("aiadmin");
		if (!$aiadmin->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $aiadmin->create(); // 保存生成的数据对象
			$data['username'] = $username;
			$data['password'] = md5($password);
			$data['type'] = $type;
			if (empty($password)) {
				unset($data['password']);
			}
			// p($data);exit();
			$aiadmin->where(array('id'=>$id))->save($data);//更新数据
			// echo $aiadmin->_sql();exit();
			$this->success('修改成功',"/Home/Aiadmin/aiadmin_list",1);
		}
	}


}